{extend name="public/container"}
{block name="content"}
<div id="myApp">
    <div class="douxue-filter padding-20 flex flex-between" style="margin-bottom:20px;">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
              <el-form-item>
                <el-select
                    size="small"
                    v-model="where.status"
                    placeholder="是否显示"
                    :clearable=true>
                    <el-option label="显示" :value="1"></el-option>
                    <el-option label="隐藏" :value="2"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-input size="small" v-model="where.title" placeholder="请输入关键字">
                    <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                </el-input>
              </el-form-item>
            </div>
        </el-form>
        <el-button type="primary" size="mini" @click="doAdd()">新增搜索词</el-button>
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            v-loading="loading"
                element-loading-text="加载中"
                element-loading-spinner="el-icon-loading"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="80"
                align="left">
            </el-table-column>
            <el-table-column
                prop="title"
                label="名称"
                align="left">
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                width="120"
                cell-click="cell"
                align="left">
            </el-table-column>
            <el-table-column
                prop="status"
                label="是否显示"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-color="#ff6a6c"
                        inactive-color="#999"
                        :active-value="1"
                        :inactive-value="2"
                        @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                label=""
                width="90"
                align="left">
                <template slot-scope="scope">
                    <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                    <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                small
                :hide-on-single-page="true"
                layout="prev, pager, next"
                :total="rowsCount"
                :page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>

    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="80px" >
            <el-form-item label="搜索名称" prop="title">
                <el-input v-model="formData.title" placeholder="搜索名称" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="formData.sort" placeholder="搜索名称" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
                <el-select v-model="formData.status" placeholder="请选择">
                    <el-option
                            v-for="item in statusOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                dialogFormVisible: false,
                formData: {
                    id: 0,
                    title: '',
                    status: 1
                },
                rules: {
                    title: [
                        { required: true, message: '搜索名称不能为空', trigger: 'blur' },
                    ]
                },
				loading: false,
                rowsCount: 0,
                rows: [],
                where:{
                    page:1,
                    limit: pageSize,
                    status:'',
                    title:'',
                },
                statusOptions: [
                    {value: 1, label: '是'},
                    {value: 2, label: '否'},
                ],
            }
        },
        methods: {
            doLoadRows (pn) {
                this.where.page = pn || 1
				this.loading = true
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
					this.loading = false
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 设置状态
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}})).then(res => {
                    this.$message({
                        message: res.data.msg,
                        type: res.data.code === 200 ? 'success' : 'error',
                        onClose: _ => {
                            if (res.data.code !== 0) {
                                this.doLoadRows()
                            }
                        }
                    })
                })
            }
            // 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    this.formData = item;
                } else {
                    this.formData = {
                        id: 0,
                        title: '',
                        status: 1
                    }
                }
                this.dialogFormVisible = true
            }
            ,closeDialog(){
                this.$refs['formData'].clearValidate();
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}